<template>
  <div v-loading="loading" class="dashboard-container">
    <div class="app-container">
      <!-- 组织架构的内容-头部 -->
      <el-card class="tree-card">
        <!-- 一行 -->
        <tree-tools
          :tree-node="company"
          :is-root="true"
          :show-dialog="showDialog"
          @addDepts="addDepts"
        />
        <el-tree
          :data="departs"
          :props="defaultProps"
          :default-expand-all="true"
        >
          <tree-tools
            slot-scope="{ data }"
            :tree-node="data"
            @delDepts="getDepartments"
            @addDepts="addDepts"
            @editDepts="editDepts"
          />
        </el-tree>
      </el-card>
    </div>
    <!-- 放置新增弹层组件  -->
    <add-dept
      ref="addDept"
      :show-dialog="showDialog"
      :tree-node="node"
      @close="close"
      @addDepts="getDepartments"
      @changedialog="showDialog = $event"
    />
  </div>
</template>

<script>
import TreeTools from "./components/tree-tools.vue";
import { getDepartments } from "@/api/departments";
import { tranListToTreeData } from "@/utils";
import AddDept from "./components/add-dept"; // 引入新增部门组件

export default {
  components: {
    TreeTools,
    AddDept,
  },
  data() {
    return {
      company: { name: "江苏传智播客教育科技股份有限公司", manager: "负责人" },
      departs: [
        {
          name: "总裁办",
          manager: "曹操",
          children: [{ name: "董事会", manager: "曹丕" }],
        },
        { name: "行政部", manager: "刘备" },
        { name: "人事部", manager: "孙权" },
      ],
      defaultProps: {
        children: "children",
        label: "name",
      },
      showDialog: false, // 显示窗体
      node: null,
      loading: false,
    };
  },
  created() {
    this.getDepartments();
  },
  methods: {
    async getDepartments() {
      this.loading = true;
      const result = await getDepartments();
      this.company = { name: result.companyName, manager: "负责人", id: "" };
      this.departs = tranListToTreeData(result.depts, "");
      // console.log(result);
      this.loading = false;
    },
    addDepts(node) {
      this.showDialog = true; // 显示弹层
      // 因为node是当前的点击的部门， 此时这个部门应该记录下来,
      this.node = node;
    },
    close() {
      this.showDialog = false;
    },
    editDepts(node) {
      // 首先打开弹层
      this.showDialog = true;
      this.node = node; // 赋值操作的节点
      // console.log(this.$refs.addDept);
      this.$refs.addDept.getDepartDetail(node.id);
    },
  },
};
</script>

<style scoped>
.el-dropdown-link {
  cursor: pointer;
}

.tree-card {
  padding: 30px 140px;
  font-size: 14px;
}
</style>
